from rest_framework.generics import CreateAPIView, ListAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.pagination import PageNumberPagination
from orders.models import OrdersModel
from orders.seriali import OrderModelSerializer, OrderListModelSerializer
# Create your views here.

# 生成订单
class OrderCreateApiView(CreateAPIView):
    permission_classes = [IsAuthenticated]
    queryset = OrdersModel.objects.all()
    serializer_class = OrderModelSerializer

# 订单状态
class OrderPayStatusChoicesApiView(APIView):
    def get(self, request):
        return Response(OrdersModel.STATUS_CHOICES)


class OrderListPageNumberPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'size'
    page_query_param = 'page'
    max_page_size = 10
class OrderListApiView(ListAPIView):
    permission_classes = [IsAuthenticated]
    pagination_class = OrderListPageNumberPagination
    # queryset =
    serializer_class = OrderListModelSerializer
    def get_queryset(self):
        user = self.request.user
        query_set = OrdersModel.objects.filter(user=user,is_deleted=False,is_show=True)

        order_status = int(self.request.query_params.get('status',-1))
        print('获取前端传递的状态:',order_status)
        # 动态获取配置的订单状态列表
        status_list = [i[0] for i in OrdersModel.STATUS_CHOICES]
        # 匹配前端传递的状态，是否存在状态列表中
        print('-----------------')
        if order_status in status_list:
            query_set = query_set.filter(order_status=order_status)
        print(query_set)
        return query_set.order_by('-id')